package com.qianli.user.domain.respository;

import com.fqgj.xjd.user.dao.UserFeatureTagDao;
import com.fqgj.xjd.user.dao.UserFeatureTagMapDao;
import com.fqgj.xjd.user.entity.UserFeatureTagEntity;
import com.fqgj.xjd.user.entity.UserFeatureTagMapEntity;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.qianli.user.domain.model.UserFeatureModel;
import com.qianli.user.domain.model.feature.UserFeatureTag;
import com.qianli.user.ro.feature.FeatureTagRO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * name:特征信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/4 10:06
 * version: v1.0.0
 **/
@Repository
public class UserFeatureRespository implements BaseRespository<UserFeatureModel>{

    @Autowired
    private UserFeatureTagMapDao userFeatureTagMapDao;

    @Autowired
    private UserFeatureTagDao userFeatureTagDao;

    /**
     * 加载特征信息
     * @param model
     */
    @Override
    public void load(UserFeatureModel model) {
        String userCode = model.getUserCode();
        List<UserFeatureTagMapEntity> userFeatureTagMapEntities = userFeatureTagMapDao.getAllUserFeatureTags(userCode);
        List<UserFeatureTagEntity> tagEntities =  userFeatureTagDao.queryAllFeatureTags(model.getAppCode(),model.getBiz());
        List<UserFeatureTag> tags = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(userFeatureTagMapEntities)){
            for(UserFeatureTagMapEntity entity:userFeatureTagMapEntities){
                for(UserFeatureTagEntity tagEntity:tagEntities){
                    if(entity.getTagId().equals(tagEntity.getId())){
                        UserFeatureTag tag = new UserFeatureTag();
                        BeanUtils.copyProperties(entity,tag);
                        tag.setName(tagEntity.getName());
                        tag.setUserTagType(tagEntity.getType());
                        tags.add(tag);
                    }
                }
            }
            model.setUserFeatureTags(tags);
        }
    }
    /**
     * 存储特征信息
     * @param model
     */
    @Override
    public void store(UserFeatureModel model) {
        String userCode = model.getUserCode();
        Set<Long> tagIds = Sets.newHashSet();
        for(UserFeatureTag tag:model.getUserFeatureTags()){
            tagIds.add(tag.getTagId());
        }
        userFeatureTagMapDao.addTagsToUser(userCode, Lists.newArrayList(tagIds));
    }

    @Override
    public void restore(UserFeatureModel model) {

    }

    /**
     * 查询所有标签
     * @param appCode
     * @param biz
     * @return
     */
    public List<FeatureTagRO> queryAllFeatureTags(String appCode, Integer biz) {
        List<UserFeatureTagEntity> tagEntities =  userFeatureTagDao.queryAllFeatureTags(appCode,biz);
        if(CollectionUtils.isEmpty(tagEntities)){
            return null;
        }
        List<FeatureTagRO> featureTagROEs = Lists.newArrayList();
        for(UserFeatureTagEntity entity:tagEntities){
            FeatureTagRO tagRO = new FeatureTagRO();
            BeanUtils.copyProperties(entity,tagRO);
            featureTagROEs.add(tagRO);
        }
        return featureTagROEs;

    }
}
